package com.jiahao.material.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期工具类
 */
@Slf4j
public class DateUtil {

    public static final String FORMAT1="yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT2="yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    public static final String FORMAT_DATE_TID="yyMMdd";
    public static final String FORMAT_DATE_TIME = "yyyyMMddHHmmss";
    public static final String FORMAT_DATE_CHECK="yyyy-MM-dd";
    public static final String FORMAT_Time="HH:mm:ss";


    public  static String dataTimeToStr(Instant time,String f){
        if (time==null){
            return "";
        }
        DateTimeFormatter dateTime=DateTimeFormatter.ofPattern(f).withZone(ZoneId.systemDefault());
        String format = dateTime.format(time);
        return format;
    }

    public static Date StrToDate(String str,String f){
        SimpleDateFormat dateFormat=new SimpleDateFormat(f);
        Date date=null;
        try {
            date = dateFormat.parse(str);
            return date;
        } catch (ParseException e) {
            log.error("日期转化失败,请检查日期格式:"+e.getMessage());
        }
        return date;
    }

    public static Date StrToDate1(String str){
        String replace = str.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat(FORMAT2);
        Date d=null;
        try {
             d = format.parse(replace);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String dateToStr(Date date,String f){
        if (date==null){
            return "";
        }
        SimpleDateFormat format=new SimpleDateFormat(f);
        String format1 = format.format(date);
        return format1;
    }


}
